package interfaces.spawnMenu.squads;

import logic.common.Squad;
import logic.common.SquadChangedEvent;
import logic.common.SquadListener;
import logic.common.player.Player;
import fileHandling.language.interfaces.SpawnMenuText;
import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.ListenerKeeperElement;
import interfaces.superWidgets.StaticContent;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;

public class SquadContent extends StaticContent {
	
	private StaticContent membersContent;
	private LanguageButton createButton, joinButton, leaveButton;
	private Squad squad;
	private Player player;
	private int membersHeight, membersY, buttonHeight;
	private ListenerKeeperElement element;
	
	public SquadContent(Player player, int width, int height, int x, int y, ListenerKeeperElement element, Squad squad) {
		super(width, height, x, y);
		this.player = player;
		this.element = element;
		this.squad = squad;
				
		buttonHeight = height / 5;
		membersHeight = height - buttonHeight;
		membersY = buttonHeight;
		membersContent = new StaticContent(width, membersHeight, 0, membersY);
		
		createButtons();
		addSquadListener();
		update();
		
		layout();
	}
	
	private void createButtons() {
		createButton = new LanguageButton(SpawnMenuText.Create_Squad, GUISource.biggestFont);
		createButton.setSize(width, height);
		createButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(player.getSquad() != null) player.leaveSquad();
				assert(squad.isEmpty());
				player.joinSquad(squad);
			}
		});
		
		joinButton = new LanguageButton(SpawnMenuText.Join_Squad, GUISource.bigFont);
		joinButton.setSize(width, buttonHeight);
		joinButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(player.getSquad() != null) player.leaveSquad();
				assert(!squad.isFull());
				player.joinSquad(squad);
			}
		});
		
		leaveButton = new LanguageButton(SpawnMenuText.Leave_Squad, GUISource.bigFont);
		leaveButton.setSize(width, buttonHeight);
		leaveButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				assert(squad.getMembers().contains(player));
				player.leaveSquad();
			}
		});
	}
	
	private void addSquadListener() {
		element.addListener(squad, new SquadListener() {
			@Override
			public void squadChanged(SquadChangedEvent event) {
				update();
			}
		});
	}
	
	public void update() {
		removeAllWidgets();
		membersContent.removeAllWidgets();
		
		if(squad.isEmpty()) addWidget(createButton);
		else {
			ImageFont labelFont = GUISource.smallFont;
			
			addWidget(membersContent);
			
			int i = 1;
			for(Player member : squad.getMembers()) {
				InterfaceLabel memberLabel = new InterfaceLabel(member.getName(), labelFont);
				memberLabel.setSize(width, buttonHeight);
				memberLabel.setY(membersHeight - (buttonHeight * i));
				membersContent.addWidget(memberLabel);
				i++;
			}
			
			if(squad.getMembers().contains(player)) addWidget(leaveButton);
			else if(!squad.isFull()) addWidget(joinButton);
		}
		
		layout();
	}
}